package com.aios.action.settings.switches;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import com.aios.entity.Switch;
import com.aios.service.SwitchService;

public class SwitchesViewAction extends AbstractSwitchesAction {

	private static Logger logger = Logger.getLogger(SwitchesViewAction.class);
	
	private static final long serialVersionUID = 1L;
	private String panelId;
	private List<Switch> switchList;
	private String timeStamp;
	
	@Autowired protected SwitchService switchService;
	
	public String execute() {
		
		if (id != null) {
			if(actionMessage != null && !actionMessage.equals("")) {
				addActionMessage(actionMessage);
			}
			try {
				this.switches = switchService.findById(id);
			} catch (Exception e) {
				// logger
				e.printStackTrace();
				actionError = getText("error.switch.notfound");
				return INPUT;
			}
		}
		else {
			actionError = getText("error.switch.notfound");
			return INPUT;
		}
		return SUCCESS;
	}
	
	public String viewSwitchStatus() {
		try {
			this.switchList = switchService.switchStatus(panelId);
			
			if(this.switchList == null) {
				logger.warn("Switch loading error panelId = " + panelId);
				return INPUT;
			}
			
			return SUCCESS;
		} catch(Exception e) {
			logger.error(e);
			return INPUT;
		}
	}
	
	public String changeOperation() {
		try {
			if(panelId != null && id != null) {
				switchService.changeSwitchStatus(panelId, id);
			} else {
				logger.error("invalid access to switches");
			}
			
		} catch(Exception e) {
			logger.error(e.toString());
		}
		
		return SUCCESS;
	}

	public String getPanelId() {
		return panelId;
	}

	public void setPanelId(String panelId) {
		this.panelId = panelId;
	}

	public List<Switch> getSwitchList() {
		return switchList;
	}

	public void setSwitchList(List<Switch> switchList) {
		this.switchList = switchList;
	}

	public String getTimeStamp() {
		return timeStamp;
	}

	public void setTimeStamp(String timeStamp) {
		this.timeStamp = timeStamp;
	} 
	
}
